#!/usr/bin/env python
#-*- coding:utf-8 -*-
from wwpy.io.Sock import SockIO
from wwpy.net.Wcp import WcpParser
from wwpy.util.Logger import logger
from Sched import Sched
from Tasker import Tasker

class Actuator(object):
    
    def __init__(self,request=None,sock=None,address=None,*args,**kwargs):
        self.sched=Sched()
        self.tasker=Tasker()
        self.parser=WcpParser(request).parse()
        self.sock=sock
        self.address=address
        self.timeout=None
        
    def reply(self,answer):
        self.io=SockIO(sock=self.sock,timeout=self.timeout)
        self.io.write(str(answer))
    
    def start_sched(self,*args,**kwargs):
        if self.parser['operation']=='START_SCHED':
            logger.info("Actuator%s begin to start sched task." % str(self.address))
            has_error=False
            try:
                self.sched.start()
                self.sched.run()
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(1)
        else:
            self.reply(1)
    
    def stop_sched(self,*args,**kwargs):
        if self.parser['operation']=='STOP_SCHED':
            logger.info("Actuator%s begin to stop sched task." % str(self.address))
            has_error=False
            try:
                self.sched.stop()
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(1)
        else:
            self.reply(1)
            
    def restart_sched(self,*args,**kwargs):
        if self.parser['operation']=='RESTART_SCHED':
            logger.info("Actuator%s begin to restart sched task." % str(self.address))
            has_error=False
            try:
                self.sched.restart()
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(1)
        else:
            self.reply(1)
            
    def show_task_records(self,*args,**kwargs):
        if self.parser['operation']=='SHOW_TASK_RECORDS':
            logger.info("Actuator%s begin to show task records task." % str(self.address))
            has_error=False
            try:
                stdout,result=self.tasker.show_task_records(kwargs.get('sep',': '))
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(stdout)
        else:
            self.reply(1)
            
    def show_failed_tasks(self,*args,**kwargs):
        if self.parser['operation']=='SHOW_FAILED_TASKS':
            logger.info("Actuator%s begin to show failed tasks task." % str(self.address))
            has_error=False
            try:
                stdout,result=self.tasker.show_failed_tasks(kwargs.get('sep',': '))
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(stdout)
        else:
            self.reply(1)
            
    def redo_failed_tasks(self,*args,**kwargs):
        if self.parser['operation']=='REDO_FAILED_TASKS':
            logger.info("Actuator%s begin to redo failed tasks task." % str(self.address))
            has_error=False
            try:
                self.tasker.redo_failed_tasks()
            except Exception:
                has_error=True
            if has_error:
                self.reply(0)
            else:
                self.reply(1)
        else:
            self.reply(1)
            